package com.example.leetcode.leetcode;

import java.util.Stack;

/**
 * @author zht
 * @date 2021/1/12 14:31
 */
public class Solution20 {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            // 入栈
            if (chars[i] == '(' || chars[i] == '[' || chars[i] == '{') {
                stack.push(chars[i]);
            }
            // 出栈
            else {
                if (stack.size()==0) {
                    return false;
                }
                char temp = stack.peek();
                if (temp == '(' && chars[i] == ')') {
                    stack.pop();
                } else if (temp == '[' && chars[i] == ']') {
                    stack.pop();
                } else if (temp == '{' && chars[i] == '}') {
                    stack.pop();
                }else {
                    return false;
                }
            }

        }
        if (stack.size() > 0) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {

    }
}
